﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CarbonExchange_QuestionText : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ds.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(ds_Select);
        ds.Update += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(ds_Update);
        grid.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(grid_ItemCommand);
        if (!IsPostBack)
        {
            if (Request.QueryString["Id"] != null)
            {
                try
                {
                    QuestionId = int.Parse(Request.QueryString["Id"]);
                }
                catch (FormatException)
                {
                }
            }
            DataBind();
        }
    }

    void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Clear")
        {
            string name = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Name"] as string;
            CarbonExchange.Bll.QuestionTextFactory factory = new CarbonExchange.Bll.QuestionTextFactory();
            var tmp = factory.GetByQuestionIdAndName(QuestionId, AcmsContext.SiteId, name);
            if (tmp != null)
            {
                factory.Delete(tmp);
                e.Item.OwnerTableView.Rebind();
            }
        }
    }

    void ds_Update(object sender, Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs e)
    {
        if (IsValid)
        {
            string name = e.Keys["Name"] as string;
            if (name != null)
            {
                CarbonExchange.Bll.QuestionTextFactory factory = new CarbonExchange.Bll.QuestionTextFactory();
                var tmp = factory.GetByQuestionIdAndName(QuestionId, AcmsContext.SiteId, name);
                tmp = tmp ?? new CarbonExchange.Bll.QuestionText(AcmsContext.SiteId, QuestionId, name);
                tmp.Text = e.NewValues["DatabaseText"] as string; 
                factory.Save(tmp);
            }

        }
    }

    void ds_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(QuestionTextNames);
    }

    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Administrator}; }
    }

    private int QuestionId { get; set; }

    protected override object SaveViewState()
    {
        ViewState["QuestionId"] = QuestionId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["QuestionId"] is int)
        {
            QuestionId = (int)ViewState["QuestionId"];
        }
        
    }

    private CarbonExchange.Bll.Question question;
    protected CarbonExchange.Bll.Question Question
    {
        get
        {
            if(question == null)
            {
                CarbonExchange.Bll.QuestionFactory factory = new CarbonExchange.Bll.QuestionFactory();
                question = factory.GetById(QuestionId);
            }
            return question;
        }
    }

    private List<CarbonExchange.Bll.QuestionText> questionTexts;
    protected List<CarbonExchange.Bll.QuestionText> QuestionTexts
    {
        get
        {
            if (questionTexts == null)
            {
                CarbonExchange.Bll.QuestionTextFactory factory = new CarbonExchange.Bll.QuestionTextFactory();
                questionTexts = factory.GetAllByQuestionId(QuestionId, AcmsContext.SiteId).ToList();
            }
            return questionTexts;
        }
    }

    private List<CarbonExchange.Bll.QuestionTextConfiguration.AuditQuestionCustomText> questionTextNames;
    protected List<CarbonExchange.Bll.QuestionTextConfiguration.AuditQuestionCustomText> QuestionTextNames
    {
        get
        {
            if (questionTextNames == null && Question != null)
            {
                CarbonExchange.Bll.QuestionTextConfiguration config = new CarbonExchange.Bll.QuestionTextConfiguration(Question, AcmsContext.SiteId);
                questionTextNames = config.TextValues;
            }
            return questionTextNames;
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.AddSettings();
        breadcrumbs.Add("Audit Questions", "~/CarbonExchange/Questions.aspx");
        if (Question != null)
        {
            breadcrumbs.Add(string.Format("{0} Text", Question.Name), "~/CarbonExchange/QuestionText.aspx?Id={0}".Format(QuestionId));
        }
    }
}
